package com.example.myapplication.adapters;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.bean.AreaDtoResponse;
import com.example.myapplication.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.MyViewHolder> {
    private List<AreaDtoResponse.ProvinceCityResDto> mDataList;
    private AreaDtoResponse.ProvinceCityResDto mUser;
    private Context mContext;
    private ResultCallback myCallback;
    private int cityPosition = -1;

    public AddressAdapter(List<AreaDtoResponse.ProvinceCityResDto> dataList, Context mContext) {
        this.mDataList = dataList;
        this.mContext = mContext;
        if(this.mDataList == null){
            this.mDataList = new ArrayList<>();
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        view = LayoutInflater.from(mContext).inflate(R.layout.item_address, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        final AreaDtoResponse.ProvinceCityResDto sc = mDataList.get(position);

        holder.tv_address.setText(sc.getCityName());
        if(cityPosition == position){
            LogUtils.i("dove","onBindViewHolder-"+cityPosition+"-"+position);
            holder.tv_address.setTextColor(Color.RED);
            holder.iv_address.setVisibility(View.VISIBLE);
        }else{
            holder.iv_address.setVisibility(View.GONE);
        }
        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myCallback.onSuccess(sc.getCityName(),sc.getId(),position);
            }
        });
    }


    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public AreaDtoResponse.ProvinceCityResDto getItem(){
        if(mUser==null){
            mUser = new AreaDtoResponse.ProvinceCityResDto();
        }
        return mUser;
    }

    public void setOnItemClick(ResultCallback resultCallback) {
        this.myCallback = resultCallback;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_address;
        private View root;
        private ImageView iv_address;

        public MyViewHolder(View itemView) {
            super(itemView);
            iv_address = itemView.findViewById(R.id.iv_address);
            tv_address =  itemView.findViewById(R.id.tv_address);
            root = itemView.findViewById(R.id.item_layout_root);
        }
    }

    public void setmDataList(List<AreaDtoResponse.ProvinceCityResDto> dataList,int mcityPosition) {
        if(dataList==null){
            dataList = new ArrayList<>();
        }
        this.cityPosition = mcityPosition;
        this.mDataList = dataList;
    }

    public static abstract class ResultCallback{
        public abstract void onSuccess(String cityName, String cityId, int position);
    }

}
